# encoding: utf-8
"""
!/usr/bin/python3
@Author: Gao Shuo
@Time: 2018/10/16 14:36 
@ReadMe:
    Input: 
    Output: 
"""

import os.path
import sys

import pandas as pd
from PIL import Image

if __name__ == "__main__":
    path = sys.argv[1]
    dest = sys.argv[2]

    width = int(sys.argv[3])
    height = int(sys.argv[4])
    li = os.listdir(path)

    # 合并：检查颜色并计算
    li_filename = []
    li_color0 = []
    li_color1 = []
    li_color_portion = []

    for filepath in li:
        filein = os.path.join(path, filepath)
        im = Image.open(filein)
        color0 = 0
        color1 = 0
        for i in range(width):
            for j in range(height):
                color = im.getpixel(xy=(i, j))
                if color == (128, 0, 0):
                    color1 += 1
                elif color == (128, 128, 128):
                    color0 += 1
        li_filename .append(filepath)
        li_color0  .append(color0)
        li_color1 .append(color1)
        li_color_portion  .append ( '%.6f' % (float(color1)/(color0+color1)))
        print ('finished: %s' % filepath)
    dic = {'img': li_filename,  'color0':li_color0, 'color1':li_color1,  'color_portion':li_color_portion}
    df = pd.DataFrame(dic ,
                      columns=['img', 'color0', 'color1', 'color_portion'])
    df.to_csv(dest)



